package com.algorithm.sort;

import java.util.Arrays;

/**
 * @program: study
 * @className: BubbleSort
 * @description: 冒泡排序
 * @author: kukuxiahuni
 * @create: 2020-01-12 13:04
 * @version: v1.0
 **/
public class BubbleSort {

    public static void main(String[] args) {
        int[] array = {4, 5, 6, 3, 2, 1};
        bubbleSort(array);
    }

    /**
     * 功能描述: 冒泡排序,升序排序
     *
     * @param:
     * @return:
     * @auther: kukuxiahuni
     * @date: 2020/1/12 1:05 下午
     * @modify_auther: kukuxiahuni
     * @modify_time: 2020/1/12 1:05 下午
     **/
    public final static void bubbleSort(int[] array) {
        if (array == null || array.length <= 1) {
            return;
        }

        /**
         * 发生交换标记
         */
        boolean swap = true;

        for (int i = 0; i < array.length; ++i) {
            swap = false;

            for (int j = 0; j < array.length - 1 - i; ++j) {
                if (array[j + 1] <= array[j]) {
                    swap(array, j, j + 1);
                    swap = true;

                }
            }

            if (!swap) {
                break;
            }
        }

        Arrays.stream(array).forEach(System.out::println);

    }

    /**
     * 功能描述: TODO
     *
     * @param:
     * @return:
     * @auther: kukuxiahuni
     * @date: 2020/1/12 1:10 下午
     * @modify_auther: kukuxiahuni
     * @modify_time: 2020/1/12 1:10 下午
     **/
    private final static void swap(int[] array, int a, int b) {
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }
}
